package com.tust.app.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.tust.app.domain.Source;
import com.tust.app.service.SourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class SourceController {

    @Autowired
    private SourceService sourceService;

    @RequestMapping("/spage")
    public String spage(HttpServletRequest request) {
        PageHelper.startPage(1, 3);
        List<Source> sources = sourceService.getAllSources();
        PageInfo page = new PageInfo(sources);
        request.setAttribute("pageInfo", page);
        return "home";
    }

    @RequestMapping("/spage/{pageNum}")
    public String spage2(@PathVariable("pageNum") Integer pageNum, HttpServletRequest request){
        PageHelper.startPage(pageNum, 3);
        List<Source> sources = sourceService.getAllSources();
        PageInfo page = new PageInfo(sources);
        request.setAttribute("pageInfo", page);
        return "home";
    }


    @RequestMapping("/asource")
    public String listAdminSources(Integer pn, HttpServletRequest request) {
        if (pn == null) {
            pn = 1;
        }
        PageHelper.startPage(pn, 3);
        List<Source> sources = sourceService.getAllSources();
        PageInfo page = new PageInfo(sources);
        request.setAttribute("sourceList", page); // Ensure attribute name matches JSP
        return "admin/source/allSource";
    }

    @PostMapping(value = "/upload")
    public String writeSource(@RequestParam("filename") String filename, HttpServletRequest request) {
        // 构造一个 Source 对象
        Source source = new Source();
        source.setFilename(filename);

        // 设置发布时间为当前时间
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String pubtime = format.format(date);
        source.setPubtime(pubtime);

        // 添加文件
        sourceService.insert(source);

        // 查询所有的文件，传递给页面
        PageHelper.startPage(1, 10);
        List<Source> sources = sourceService.getAllSources();
        PageInfo<Source> pageInfo = new PageInfo<>(sources);
        request.setAttribute("sourceList", pageInfo);

        return "admin/source/allSource";
    }

//    @RequestMapping("/selectByFilename")
//    public String selectByFilename(String filename, HttpServletRequest request){
//        // 根据filename去查询相关的文件
//        PageHelper.startPage(1, 8);
//        List<Source> searchList = sourceService.getSourcesByFilename(filename);
//        PageInfo pageInfo = new PageInfo(searchList);
//        request.setAttribute("sourceList", pageInfo);
//        if (searchList != null){
//            return "admin/source/sourceList";
//        }
//        return "error";
//    }
//
//    @RequestMapping("/getSource")
//    public String getSource(Integer id, HttpServletRequest request){
//        // 根据id获取对应文件的信息
//        Source source = sourceService.getSourceById(id);
//        // 把查询出来的source对象传递到页面上去
//        request.setAttribute("source", source);
//        return "admin/source/editsource";
//    }
//
//    @RequestMapping("/updateSource")
//    public String updateSource(Integer id, String filename, String pubtime){
//        // 实现修改的过程
//        Source source = new Source();
//        source.setId(id);
//        source.setFilename(filename);
//        source.setPubtime(pubtime);
//        boolean b = sourceService.updateSource(source);
//        if (b){
//            return "forward:/asource";
//        }
//        return "error";
//    }
//
    @RequestMapping("/delete")
    public String delete(Integer id){
        // 完成删除的功能
        boolean b = sourceService.delSource(id);
        if (b){
            return "forward:/asource";
        }
        return "error";
    }
//
//    @RequestMapping("/selectSourceById")
//    public String selectSourceById(Integer id, HttpServletRequest request){
//        // 根据id查询一条文件消息
//        Source source = sourceService.getSourceById(id);
//
//        // 把source对象传递给页面，进行显示
//        request.setAttribute("source", source);
//        return "detail";
//    }
}